//
//  SubscribeListCell.m
//  suRead
//
//  Created by lidi on 2018/11/29.
//  Copyright © 2018 bao. All rights reserved.
//

#import "SubscribeListCell.h"
#import "M_Subscribe.h"
#import "UIImageView+WebCache.h"
#import "UIControl+Blocks.h"

@interface SubscribeListCell()
@property (weak, nonatomic) IBOutlet UIImageView *imageViewIcon;
@property (weak, nonatomic) IBOutlet UILabel *labelName;
@property (weak, nonatomic) IBOutlet UILabel *labelCount;
@property (weak, nonatomic) IBOutlet UIButton *buttonSubscribe;
@property (copy, nonatomic) SubscribeHandle handle;
@end

@implementation SubscribeListCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
    self.imageViewIcon.layer.cornerRadius = 20.0f;
    self.imageViewIcon.layer.masksToBounds = true;
    self.buttonSubscribe.layer.cornerRadius = 5.0f;
    self.buttonSubscribe.layer.masksToBounds = true;
}

- (void)refresh:(M_Subscribe *)sub handle:(nonnull SubscribeHandle)handle {
    self.handle = handle;
    
    [self.imageViewIcon sd_setImageWithURL:[NSURL URLWithString:sub.f_service_img] placeholderImage:nil];
    self.labelName.text = sub.f_service_name;
    self.labelCount.text = [NSString stringWithFormat:@"文章%@篇",sub.f_service_count];
    
    UIColor *red = [[UIColor alloc] initWithRed:190.0f / 255.0f green:30.0f / 255.0f blue:30.0f / 255.0f alpha:1];
    if (sub.f_is_subscribe) {
        //已订阅
        [self.buttonSubscribe setTitle:@"已订阅" forState:UIControlStateNormal];
        [self.buttonSubscribe setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        self.buttonSubscribe.backgroundColor = red;
        self.buttonSubscribe.layer.borderWidth = 0;
    } else {
        //未订阅
        [self.buttonSubscribe setTitle:@"订阅" forState:UIControlStateNormal];
        [self.buttonSubscribe setTitleColor:red forState:UIControlStateNormal];
        self.buttonSubscribe.backgroundColor = [UIColor whiteColor];
        self.buttonSubscribe.layer.borderColor = red.CGColor;
        self.buttonSubscribe.layer.borderWidth = 1.0f;
    }
    [self.buttonSubscribe removeAllActions];
    
    __weak typeof (self) weakSelf = self;
    [self.buttonSubscribe addActionForControlEvents:UIControlEventTouchUpInside usingBlock:^(UIControl *sender, UIEvent *event) {
        weakSelf.handle(!sub.f_is_subscribe);
    }];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
